import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import cloneDeep from 'lodash.clonedeep'
import modules from './modules'
import { PACKAGE_NAME as key } from '@/config/const'
import { LocalStorageService } from '@/utils/storage'

Vue.use(Vuex)

// 初始化纯净版本 存储一份，使用options的时候注意不要更改其内任何引用
const options = {
  modules: cloneDeep(modules)
}

const store = new Vuex.Store(
  {
    ...cloneDeep(options),
    plugins: [createPersistedState({ key })]
  }
)

export default store

// 重置store
export function resetStore() {
  const cacheModules = cloneDeep(options.modules)
  const initStateData = {}
  Object.keys(cacheModules).forEach((key) => {
    initStateData[key] = cacheModules[key].state
  })
  store.replaceState(initStateData)
  LocalStorageService.set(key, initStateData)
}